package com.xdq.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdq.commonutils.R;
import com.xdq.servicebase.exceptionhandler.GuliException;
import com.xdq.serviceedu.entity.EduCourse;
import com.xdq.serviceedu.entity.EduTeacher;
import com.xdq.serviceedu.entity.vo.CourseInfoVo;
import com.xdq.serviceedu.entity.vo.CoursePublishVo;
import com.xdq.serviceedu.entity.vo.CourseQuery;
import com.xdq.serviceedu.entity.vo.TeacherQuery;
import com.xdq.serviceedu.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @PostMapping("addCourseInfo")
    public R addcourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = null;
        try {
            id = courseService.saveCourseInfo(courseInfoVo);
        } catch (GuliException e) {
            return R.error();
        }
        return R.ok().data("courseId", id);
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "分页查询讲师列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageListCourse(@PathVariable long current, @PathVariable long limit,
                             @RequestBody(required = false) CourseQuery courseQuery) {

        Map<String, Object> data = courseService.pageCondition(current, limit, courseQuery);
        return R.ok().data(data);
    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        try {
            courseService.removeCourse(courseId);
            return R.ok();
        } catch (GuliException e) {
            return R.error().message(e.getMessage());
        }

    }

}

